package Java8.optional;

import java.util.Optional;

public class Test {

    public static void main(String[] args) {
        person p = new person();
        p.setAge(1);
        p.getName();

        person p2 = null;

        //Optional.ofNullable(p2).ifPresent(e->{throw new RuntimeException("sss");});
        //Optional.ofNullable(p).ifPresent(e->{throw new RuntimeException("p  sss");});
        System.out.println(Optional.ofNullable(p2).map(e -> "p2 not null" + e.getAge()).orElse("p2 is null"));

        System.out.println(Optional.ofNullable(p).map(e -> "p mnot null" + e.getAge()).orElse("p is null"));

        Optional optional = Optional.empty();
        System.out.println((person) optional.orElse(null));
        System.out.println("666");

        person djf = new person();
        djf.setName("d");
        djf.setAge(100);
        Optional<person> optionalPerson = Optional.ofNullable(djf);

        System.out.println(optionalPerson.isPresent());
        optionalPerson.filter(_person -> _person.getName().equals("d")).ifPresent(_person -> System.out.println(_person.getAge()));

        Optional<person> optionalNull = Optional.ofNullable(null);
        optionalNull.filter(_person -> _person.getName().equals("d")).ifPresent(_person -> System.out.println(_person.getAge()));
        String isNullPerson = optionalNull.map(_person -> _person.getName()).orElse(getStringName());
        String isNullPerson3 = optionalNull.map(_person -> _person.getName()).orElse("8888");
        String stringPerson = optionalPerson.map(_person -> _person.getName()).orElse(getStringName());
        String stringPerson2 = optionalPerson.map(_person -> _person.getName()).orElse("999");
        System.out.println(isNullPerson);
        System.out.println("finish...");
    }

    private static String getStringName() {
        return "jjj";
    }
}

class person {

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}